<template>
  <!-- 黑件重量记录表单弹窗 -->
  <el-dialog
          v-model="visibleDialog"
          :title="props.title"
          width="70%"
          draggable
          @close="closeDialog"
  >
  <el-descriptions
      class="margin-top"
      title=""
      :column="2"
      size="large"
      v-show="props.isReadonly"
      border
    >
    <el-descriptions-item>
        <template #label>
          <div class="cell-item">年</div>
        </template>
        <div style="width: 50%; word-break: break-all">
          {{formData.planYear}}
        </div>
      </el-descriptions-item>
      <el-descriptions-item>
        <template #label>
          <div class="cell-item">月</div>
        </template>
        <div style="width: 50%; word-break: break-all">
          {{formData.planMonth}}
        </div>
      </el-descriptions-item>
      <el-descriptions-item>
        <template #label>
          <div class="cell-item">实际重量</div>
        </template>
        <div style="width: 50%; word-break: break-all">
          {{formData.actualWeight}}
        </div>
      </el-descriptions-item>
  
    </el-descriptions>
    <el-form ref="formRef" :model="formData" :rules="rules" label-width="160" v-show="!props.isReadonly">
      <el-row>
      <el-col :span="12">
      <el-form-item label="年" prop="planYear" label-width="80px">
      <el-input v-model="formData.planYear" placeholder="请输入年" clearable/>
        </el-form-item>
      </el-col>
      <el-col :span="12">
      <el-form-item label="月" prop="planMonth" label-width="80px">
      <el-input v-model="formData.planMonth" placeholder="请输入月" clearable/>
        </el-form-item>
      </el-col>
      <el-col :span="12">
      <el-form-item label="实际重量" prop="actualWeight" label-width="80px">
      <el-input v-model="formData.actualWeight" placeholder="请输入实际重量" clearable/>
        </el-form-item>
      </el-col>
      </el-row>
    </el-form>

    <template #footer>
      <div class="dialog-footer">
        <el-button v-if="!props.isReadonly" type="primary" :loading="loading" @click="handleSubmit">确 定</el-button>
        <el-button @click="closeDialog">取 消</el-button>
      </div>
    </template>
  </el-dialog>
</template>

<script setup lang="ts">
  import request from "@/utils/request";
  import { useDictStoreHook } from "@/store/modules/dictionary"; // 引入
  const dictionaryStore = useDictStoreHook();

  defineOptions({
    name: "MESMESBLACKPIECEWEIGHTRECORDADDORUPDATE",
    inheritAttrs: false,
  });

  const emit = defineEmits(["reset-table", "change-visible"]);

  const props = defineProps({
    visible: {
      type: Boolean,
      default: () => {
        return false;
      },
    },
    title: {
      type: String,
      default: () => {
        return "";
      },
    },
    data: {
      type: Object,
      default: () => {
        return {};
      },
    },
    isReadonly: {
      type: Boolean,
      default: () => {
        return false;
      },
    },
  });

  const visibleDialog = ref(false);
  watch(
      () => props.visible,
      (newVal: boolean) => {
        visibleDialog.value = newVal;
        setTimeout(() => {
          formRef.value.clearValidate();
        }, 50);
      }
  );

  const rules = reactive({
    planYear: [
                        ],
    planMonth: [
                        ],
    actualWeight: [
                        ],
  });

  const formRef = ref(ElForm);

  let formData = reactive({
 id: "",  planYearMonth: "",  planYear: "",  planMonth: "",  actualWeight: "",  version: "",  remarks: "",  createTime: "",  createdUserId: "",  createdUserName: "",  lastUpdateTime: "",  lastUpdatedUserId: "",  lastUpdatedUserName: "",  enabled: "",  deleted: "",   });

  watch(
          () => props.data,
          (newVal: any) => {
            formData.id = ""
            if (newVal?.id) {
              const {  id,  planYearMonth,  planYear,  planMonth,  actualWeight,  version,  remarks,  createTime,  createdUserId,  createdUserName,  lastUpdateTime,  lastUpdatedUserId,  lastUpdatedUserName,  enabled,  deleted,  } = newVal;
              Object.assign(formData, {  id,  planYearMonth,  planYear,  planMonth,  actualWeight,  version,  remarks,  createTime,  createdUserId,  createdUserName,  lastUpdateTime,  lastUpdatedUserId,  lastUpdatedUserName,  enabled,  deleted,  });
            }
          }
  );

  /** 关闭表单弹窗 */
  function closeDialog() {
    visibleDialog.value = false;
    emit("change-visible", false);
    resetForm();
  }

  /** 重置表单 */
  function resetForm() {
    formRef.value.resetFields();
    formRef.value.clearValidate();
    formData.id = ""
    formData.planYearMonth = ""
    formData.planYear = ""
    formData.planMonth = ""
    formData.actualWeight = ""
    formData.version = ""
    formData.remarks = ""
    formData.createTime = ""
    formData.createdUserId = ""
    formData.createdUserName = ""
    formData.lastUpdateTime = ""
    formData.lastUpdatedUserId = ""
    formData.lastUpdatedUserName = ""
    formData.enabled = ""
    formData.deleted = ""
  }

  const loading = ref(false);

  /** 黑件重量记录保存提交 */
  function handleSubmit() {
    formRef.value.validate(async (valid: any) => {
      if (valid) {
        loading.value = true;
        const text = formData.id ? "编辑" : "新增";
        await request({
          url: "/mes/mesblackpieceweightrecord/save",
          method: "post",
          data: formData,
        }).finally(() => {
          loading.value = false;
        });
        ElMessage.success( text +`成功`);
        closeDialog();
        emit("reset-table", formData.id);
      }
    });
  }


  // 组件销毁时调用
  onBeforeUnmount(() => {
    visibleDialog.value = false;
    emit("change-visible", false);
  });


</script>
